#include <string.h>
#include <stddef.h>


#include "ringbuffer.h"

#define MIN(a, b) ((a) < (b))? (a):(b)


/*********************************************************************************************
** Function name:           ring_buf_init
** Descriptions:            构造一个空环形缓冲区
** input parameters:        r: 环形缓冲区管理器
                            buf: 数据缓冲区
                            len: buf长度
** output parameters:       none
** Returned value:          1：初始化成功 0：初始化失败
** Created by:              zheh.zhang
** Created date:            2021-09-08
**--------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**********************************************************************************************/
bool ring_buf_init(ring_buf_t *r, unsigned char *buf, unsigned int len)
{
    r->buf = buf;
    r->size = len;
    r->front = r->rear = 0;

    return (buf != NULL && (len & len -1)) == 0;
}

/*********************************************************************************************
** Function name:           ring_buf_clr
** Descriptions:            清空环形缓冲区
** input parameters:        r: 环形缓冲区管理器
** output parameters:       none
** Returned value:          none
** Created by:              zheh.zhang
** Created date:            2021-09-08
**--------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**********************************************************************************************/
void ring_buf_clr(ring_buf_t *r)
{
    r->front = r->rear = 0;
}

/*********************************************************************************************
** Function name:           ring_buf_len
** Descriptions:            获取环形缓冲区数据长度
** input parameters:        r: 环形缓冲区管理器
** output parameters:       none
** Returned value:          环形缓冲区中有效字节数
** Created by:              zheh.zhang
** Created date:            2021-09-08
**--------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**********************************************************************************************/
unsigned int ring_buf_len(ring_buf_t *r)
{
    return ((r->rear - r->front)&(r->size - 1));  
}

/*********************************************************************************************
** Function name:           ring_buf_is_full
** Descriptions:            判断缓存buf是否已满
** input parameters:        r: 环形缓冲区管理器
** output parameters:       none
** Returned value:          1：缓存区已满 0：缓冲区未满
** Created by:              zheh.zhang
** Created date:            2021-12-16
**--------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**********************************************************************************************/
unsigned int ring_buf_is_full(ring_buf_t *r)   
{
    return (((r->rear - r->front)&(r->size - 1)) == (r->size - 1));
}

/*********************************************************************************************
** Function name:           ring_buf_is_empty
** Descriptions:            判断缓存buf是否为空
** input parameters:        r: 环形缓冲区管理器
** output parameters:       none
** Returned value:          1：缓存区为空 0：缓冲区不为空
** Created by:              zheh.zhang
** Created date:            2021-12-16
**--------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**********************************************************************************************/
unsigned int ring_buf_is_empty(ring_buf_t *r)   
{
    return (r->front == r->rear);
}

/*********************************************************************************************
** Function name:           ring_buf_put_char
** Descriptions:            将单个字节放入缓冲区
** input parameters:        r: 环形缓冲区管理器
                            data: 缓存单个字节数据
** output parameters:       none
** Returned value:          1：存入成功 0：存入失败
** Created by:              zheh.zhang
** Created date:            2021-12-16
**--------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**********************************************************************************************/
unsigned int ring_buf_put_char(ring_buf_t *r, unsigned char data)
{
    if(ring_buf_is_full(r)) {
        return 0;
    }

    r->buf[r->rear] = data;
    r->rear = ((r->rear + 1)&(r->size - 1));

    return 1;
}

/*********************************************************************************************
** Function name:           ring_buf_get_char
** Descriptions:            从缓冲区取出单个字节
** input parameters:        r: 环形缓冲区管理器
                            data: 取出单个字节数据
** output parameters:       none
** Returned value:          1：存入成功 0：存入失败
** Created by:              zheh.zhang
** Created date:            2021-12-16
**--------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**********************************************************************************************/
unsigned int ring_buf_get_char(ring_buf_t *r,unsigned char *data)
{
    if(ring_buf_is_empty(r)){
        return 0;
    }

    *data = r->buf[r->front];
    r->front = ((r->front + 1)&(r->size - 1));

    return 1;
}


/*********************************************************************************************
** Function name:           ring_buf_put
** Descriptions:            将指定长度的数据放到环形缓冲区中
** input parameters:        r: 环形缓冲区管理器
                            buf: 数据缓冲区
                            len: 缓冲区长度
** output parameters:       none
** Returned value:          环形缓冲区中有效字节数
** Created by:              zheh.zhang
** Created date:            2021-09-08
**--------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**********************************************************************************************/
unsigned int ring_buf_put(ring_buf_t *r,unsigned char *buf,unsigned int len)
{
    unsigned int ret = 0;
    unsigned int i = 0;    

    if(ring_buf_is_full(r)) {
        return 0;
    }

    for(i=0; i<len; i++) {
        ret = ring_buf_put_char(r,buf[i]);
        if(!ret) break;
    }

    return i;
}


/*********************************************************************************************
** Function name:           ring_buf_get
** Descriptions:            从环形缓冲区中读取指定长度的数据
** input parameters:        r: 环形缓冲区管理器
                            len: 读取长度
** output parameters:       buf: 输出数据缓冲区
** Returned value:          环形缓冲区中有效字节数
** Created by:              zheh.zhang
** Created date:            2021-09-08
**--------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**********************************************************************************************/
unsigned int ring_buf_get(ring_buf_t *r,unsigned char *buf,unsigned int len)
{
    unsigned int count = 0;
    unsigned char *buf_ptr = buf;

    if(ring_buf_is_empty(r)){
        return 0;
    }

    while((count < len)&(ring_buf_get_char(r, buf_ptr))) {
        count++;
        buf_ptr++;
    }

    return count;

}

